Page({

  /**
   * 页面的初始数据
   */
  data: {
    mobile: "",
    show: false,
    getdata: {},
    key: ''
  },
  onLoad: function(){
    this.getKey()
},
getKey() {
  let that = this
  wx.cloud.callFunction({
    name: 'getKey',
    data: {
      name: 'phone'
    }
  }).then(res => {
    console.log(res.result.data[0].key)
    that.setData({
      key: res.result.data[0].key
    })
  })
},
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 获取用户输入 
   */
  mobile: function (e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  /**
   *用户点击获取数据事件 
   */
  getdata: function (e) {
    var that = this;
    wx.showLoading({
      title: '加载中',
    });
    var mobile = this.data.mobile;
    var reg = /^1[3|4|5|7|8][0-9]\d{8}$/;
    if (reg.test(mobile) === false) {
      wx.showToast({
        title: '输入不合法',
        icon: 'none',
        image: '../../images/error.png',
        duration: 2000
      })
      return false;
    }
    // 请求数据
    wx.request({
      url: "https://apis.juhe.cn/mobile/get?phone=" + mobile + "&key="+that.data.key,
      data: {
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.resultcode == "200") {
          that.setData({
            show: true,
            getdata: res.data.result
          })
        } else {
          wx.showModal({
            title: '提示',
            content: "查询失败",
            success: function (res) {
              that.setData({
                show: false
              })
            }
          })
        }
      }
    })
  }
})